package org.amos.upms.modules.system.feign;

import cn.hutool.core.util.StrUtil;
import lombok.RequiredArgsConstructor;
import org.amos.api.upms.dto.DictDetailDTO;
import org.amos.api.upms.feign.IDictFeignClient;
import org.amos.core.basic.utils.AmosUtils;
import org.amos.core.basic.vo.R;
import org.amos.upms.modules.system.entity.DictDetail;
import org.amos.upms.modules.system.service.IDictDetailService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequiredArgsConstructor
public class DictFeignClient implements IDictFeignClient {
    private final IDictDetailService dictDetailService;
    @Override
    @GetMapping(API_PREFIX + "/getList")
    public R<List<DictDetailDTO>> getList(String code) {
        DictDetailDTO dto = new DictDetailDTO();
        dto.setDictCode(code);
        List<DictDetail> list = dictDetailService.selectList(dto);
        return R.ok(AmosUtils.listCopy(list, DictDetailDTO.class));
    }

    @Override
    @GetMapping(API_PREFIX + "/getValue")
    public R<String> getValue(String code, String key) {
        DictDetail one = dictDetailService.getOne(code, key);
        return one == null ? R.ok(StrUtil.EMPTY) : R.ok(one.getName());
    }
}
